<template>
  <div class="main">
    <div class="head">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="奖项：" width="100px">
          <div class="head-input">
            <el-input v-model="formInline.title" placeholder="输入奖项名称"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="学生：">
          <div class="head-input">
            <el-input v-model="formInline.studentName" placeholder="输入学生姓名"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="老师：">
          <div class="head-input">
            <el-input v-model="formInline.teacherName" placeholder="输入老师姓名"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="进度：">
          <el-select v-model="formInline.status" placeholder="请选择奖项目前进度">
            <el-option label="未申请" value="未申请" />
            <el-option label="已申请" value="已申请" />
            <el-option label="未答辩" value="未答辩" />
            <el-option label="已答辩" value="已答辩" />
            <el-option label="已出结果" value="已出结果" />
          </el-select>
        </el-form-item>
        <el-form-item label="结果：">
          <el-select v-model="formInline.result" placeholder="请选择奖项目前结果">
            <el-option label="无结果" value="无结果" />
            <el-option label="未通过" value="未通过" />
            <el-option label="已通过" value="已通过" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="body">
      <el-table v-if="tableData.length > 0"
                :data="tableData"
                height="510"
                highlight-current-row
                style="width: 90%;cursor: pointer"
                border
      >
        <el-table-column prop="title" label="奖项名称"  />
        <el-table-column prop="studentName" label="申请学生" />
        <el-table-column prop="teacherName" label="审核老师" />
        <el-table-column label="当前进度" >
          <template #default="scope">
            <el-tag :type="changeColor(scope.row.status)">{{scope.row.status}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="当前结果" >
          <template #default="scope">
            <el-tag :type="changeColor(scope.row.result)">{{scope.row.result}}</el-tag>
          </template>
        </el-table-column>
<!--        <el-table-column label="重置">-->
<!--          <template #default="scope">-->
<!--            <Edit style="width: 2em; height: 2em;" @click="changeStudentAward(scope.row)" color="#409EFC"/>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="管理员审核">
          <template #default="scope">
            <Checked style="width: 2em; height: 2em;" @click="checkedStudentAward(scope.row)" color="#409EFC"/>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="foot">
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="paginations.page_index"
                     :page-sizes="paginations.page_sizes"
                     :page-size="paginations.page_size"
                     :layout="paginations.layout"
                     :total="paginations.total">
      </el-pagination>
    </div>
<!--    重置弹窗-->
    <el-dialog
        v-model="resetDialogVisible"
        title="重置进度提示"
        width="30%"
    >
      <span>重置进度功能开发中</span>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="resetDialogVisible = false">关闭</el-button>
        <el-button type="danger" @click="resetDialogVisible = false" disabled>
          重置
        </el-button>
      </span>
      </template>
    </el-dialog>
<!--    审核弹窗-->
    <el-dialog
        v-model="checkedDialogVisible"
        title="审核信息提示"
        width="30%"
    >
      <div v-if="!checkFlag">
        <div v-if="(this.currentCheckStudentAward.result==='无结果')">
          教师还未完成最终审核
        </div>
        <div v-if="!(this.currentCheckStudentAward.result==='无结果')">
          <el-button @click="updateCheck('通过')">通过</el-button><el-button @click="updateCheck('未通过')">未通过</el-button>
        </div>
      </div>
      <div v-if="checkFlag">
        {{this.currentCheckStatus}}
      </div>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="checkedDialogVisible = false">关闭</el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import {
  SelectAllStudentAward,
  SelectAllStudentAwardBySecondInfo,
  updateStudentAwardsByForm
} from "../../api/studentAwards";
import {Checked} from "@element-plus/icons-vue";
import {queryCheckStatusByStudentAwardsId, updateCheckStatus} from "../../api/check";
import {addNotice} from "../../api/notice";

export default {
  name: "AdminQuery",
  components: {Checked},
  data() {
    return {
      resetDialogVisible:false,//重置弹窗变量
      checkedDialogVisible:false,//审核弹窗变量
      currentCheckStudentAward:{},//当前审核按钮对应学生奖项
      formInline:{
        studentName:'全部学生',
        status:'全部进度',
        result:'全部结果',
        title:'全部奖项',
        teacherName:'全部老师'
      },
      paginations: {
        page_index: 1, //当前页面
        total: 0,      //总页数
        page_size: 5,  //每页显示的记录条数
        page_sizes: [5, 10, 15, 20],  //可选择的page_size范围
        layout: "total, sizes, prev, pager, next, jumper"  //翻页属性
      },
      tableData: [],
      allTableData: [],
      check:{
        studentAwardsId:'',
        checkPeopleId:'',
        checkStatus:'',
      },//提交的审核表中审核元素
      currentCheckStatus:'',//当前选中行元素审核状态
      checkFlag:false,//审核标志 flag表示未审核，true表示已经审核过
      form:{
        id:'',
        awardId:'',
        status:'',
        result:'',
        studentId:'',
      },//管理员审核不通过发回重审
      notice:{
        content:'',
        publisherId:this.$store.state.user.id,
        recipientId:'',
        theme:'',
      },
      noticeFlag:false,
    };
  },
  created() {
    this.getProfile();
  },
  methods: {
    //得到全部的学生奖项
    getProfile() {
      SelectAllStudentAward().then(response=>{
        this.allTableData = response.data
        this.setPaginations();
      })
    },
    //重置按钮
    changeStudentAward(row){
      console.log(row);
      this.resetDialogVisible = true;
    },
    //审核标志
    checkedStudentAward(row){
      this.checkedDialogVisible = true;
      this.currentCheckStudentAward = row;
      queryCheckStatusByStudentAwardsId(row.studentAwardsId).then(res => {
        this.currentCheckStatus = res.data;
        if(this.currentCheckStatus==="未审核")
        {
          this.checkFlag = false;
        }else{
          this.checkFlag = true;
        }
      })
    },
    //审核弹窗中通过不通过按钮
    updateCheck(temp){
      this.check.studentAwardsId = this.currentCheckStudentAward.studentAwardsId;
      this.check.checkPeopleId = 1;
      this.check.checkStatus = temp;
      this.notice.theme = temp;
      this.notice.recipientId = this.currentCheckStudentAward.teacherId;
      if(temp==="未通过")
      {
        this.notice.content = "你审核的"+this.currentCheckStudentAward.studentName+"申请的"+this.currentCheckStudentAward.title+"未通过管理员最终审核，已发回重审";
        addNotice(this.notice);
        this.checkedDialogVisible = false;
        this.updateForm();
      }else{
        this.notice.content = "你审核的"+this.currentCheckStudentAward.studentName+"申请的"+this.currentCheckStudentAward.title+"通过管理员最终审核，已反馈给学生";
        addNotice(this.notice);
        updateCheckStatus(this.check).then(res => {
          this.checkedDialogVisible = false;
          this.$notify({
            title: '成功',
            message: "更新成功"+res.message,
            type: 'success'
          })
        });
      }
    },
    //审核不通过发回重审，状态变为未申请，结果变为无结果更新奖项进度
    updateForm(){
      this.form.id = this.currentCheckStudentAward.studentAwardsId;
      this.form.studentId = this.currentCheckStudentAward.studentId;
      this.form.status = "已申请";
      this.form.result = "无结果";
      this.form.awardId = this.currentCheckStudentAward.awardId;
      updateStudentAwardsByForm(this.form).then(
          this.$notify({
            title: '成功',
            message: "已发回重审",
            type: 'success'
          })
      )
    },
    changeColor(result){
      if (result==="未通过")
      {
        return 'danger';
      }else if((result === '通过')||(result === '已出结果'))
      {
        return 'success';
      }else if((result === '无结果')||(result === '未申请'))
      {
        return 'info';
      }else {
        return 'warning';
      }
    },
    //查询通过表单
    onSubmit(){
      if(this.formInline.studentName === '')
      {
        this.formInline.studentName = '全部学生';
      }
      if(this.formInline.teacherName === '')
      {
        this.formInline.teacherName = '全部老师';
      }
      if(this.formInline.status === '')
      {
        this.formInline.status = '全部进度';
      }
      if(this.formInline.result === '')
      {
        this.formInline.result = '全部结果';
      }
      if(this.formInline.title === '')
      {
        this.formInline.title = '全部奖项';
      }
      SelectAllStudentAwardBySecondInfo(this.formInline.title,this.formInline.studentName,this.formInline.status,this.formInline.result,this.formInline.teacherName).then(response=>{
        this.allTableData = response.data
        this.setPaginations();
      })
    },
    handleSizeChange(page_size) {
      this.paginations.page_index = 1;
      this.paginations.page_size = page_size;
      this.tableData = this.allTableData.filter((item, index) => {
        return index < page_size;
      });
    },
    handleCurrentChange(page) {
      this.paginations.page_index =page;
      let index = this.paginations.page_size * (page - 1);
      let nums = this.paginations.page_size * page;
      let tables = [];   // 存储跳转页面要显示的记录

      for (let i = index; i < nums; i++) {
        if (this.allTableData[i]) {
          tables.push(this.allTableData[i]);
        }
        this.tableData = tables;
      }
    },
    setPaginations() {
      this.paginations.total = this.allTableData.length;
      this.paginations.page_index = 1;
      this.paginations.page_size = 5;
      //设置默认的分页数据
      this.tableData = this.allTableData.filter((item, index) => {
        return index < this.paginations.page_size;
      });
    },
  }
}

</script>
<style scoped>
.main {
  display: flex;
  flex-direction: column;
}
.head{
  margin-top: 20px;
}
.body {
  display: flex;
  justify-content: center;
}
</style>
